<template>
    <el-container>
        <el-main class="wrapper">
            <section class="panel cms-content">
                <div class="panel-body">
                    <div class="cms-btn-group">
                        <el-tabs v-model="activeName" @tab-click="handleClick">
                            <el-tab-pane label="春秋美文" name="0">春秋美文</el-tab-pane>
                            <el-tab-pane label="鸳鸯图文" name="1">鸳鸯图文</el-tab-pane>
                            <el-tab-pane label="大队长视频" name="2">大队长视频</el-tab-pane>
                            <el-tab-pane label="小西瓜TV" name="3">小西瓜TV</el-tab-pane>
                        </el-tabs>
                    </div>

                    <div class="cms-search-group">
                        <div class="cms-left-btn">
                            <el-button type="primary" icon="fa fa-refresh" @click="list()" size="small">刷新</el-button>
                            <el-button type="primary" icon="fa fa-plus" @click="create()" size="small"> 新增</el-button>
                            <el-button type="danger" icon="fa fa-trash" @click="multidel()" size="small"> 删除</el-button>
                        </div>
                        <div class="cms-search-item">
                            <el-input  size="medium" v-model="params.title" placeholder="请输入视频名称" class="input-with-select">
                                <el-button type="primary" @click="search()" slot="append" icon="el-icon-search"></el-button>
                            </el-input>
                        </div>
                        <div class="cms-search-item">
                            <el-date-picker size="medium"
                                    v-model="date"
                                    align="right"
                                    type="daterange"
                                    unlink-panels
                                    range-separator="至"
                                    start-placeholder="开始日期"
                                    end-placeholder="结束日期"
                                    :picker-options="pickerOptions" @change="pickerChange">
                            </el-date-picker>
                        </div>
                        <div class="cms-search-item" style="width: 130px">
                            <el-select size="medium" v-model="params.userid">
                                <el-option label="管理员(全部)" value=""></el-option>
                                <el-option v-for="(manage,index) in admins" :key="index" :label="manage" :value="index"></el-option>
                            </el-select>
                        </div>
                        <div class="cms-search-item" style="width: 120px">
                            <el-select size="medium" v-model="params.hot">
                                <el-option label="热门(全部)" value=""></el-option>
                                <el-option label="热门" :value="1"></el-option>
                                <el-option label="非热门" :value="2"></el-option>
                            </el-select>
                        </div>
                        <div class="cms-search-item" style="width: 120px">
                            <el-select size="medium" v-model="params.lock">
                                <el-option label="锁定(全部)" value=""></el-option>
                                <el-option label="锁定" :value="1"></el-option>
                                <el-option label="正常" :value="2"></el-option>
                            </el-select>
                        </div>
                        <div class="cms-search-item" style="width: 120px">
                            <el-select size="medium" v-model="params.sort">
                                <el-option label="排序(默认)" value=""></el-option>
                                <el-option label="浏览量" value="1"></el-option>
                                <el-option label="分享量" value="2"></el-option>
                            </el-select>
                        </div>
                    </div>
                    <el-table ref="multipleHandle"
                              v-loading="loading"
                              element-loading-text="努力加载中..."
                              element-loading-spinner="el-icon-loading"
                              @selection-change="handleSelectionChange"
                              tooltip-effect="dark" :data="listData.data"  border>
                        <el-table-column align="center" type="selection" width="50"></el-table-column>
                        <el-table-column align="center" prop="id" label="编号" width="80"></el-table-column>
                        <el-table-column prop="thumb" label="封面" width="120">
                            <template slot-scope="scope">
                                <el-popover placement="right" trigger="click">
                                    <img :src="scope.row.thumb" />
                                    <el-button slot="reference" style="border: none;height: 50px;padding: 0;overflow: hidden">
                                        <div v-if="scope.row.hot == 1" class="ribbon">
                                            <div class="wrap"><span class="ribbon6">热</span></div>
                                        </div>
                                        <img title="点击查看原图" alt="点击查看原图" :src="scope.row.thumb" style="height: 50px;width: 100px"/>
                                    </el-button>
                                </el-popover>
                            </template>
                        </el-table-column>
                        <el-table-column label="标题" width="300">
                            <template slot-scope="scope">
                                <el-tag v-if="scope.row.lock == 0" type="primary" size="small">正常</el-tag>
                                <el-tag v-if="scope.row.lock == 1" type="danger" size="small">锁定</el-tag>
                                {{scope.row.title}}
                            </template>
                        </el-table-column>
                        <el-table-column align="center" prop="name" label="分类" width="70"></el-table-column>
                        <el-table-column align="center" prop="nickname" label="管理员" width="70"></el-table-column>
                        <el-table-column align="center" prop="share" label="分享量" width="80"></el-table-column>
                        <el-table-column align="center" prop="view" label="浏览量" width="80"></el-table-column>
                        <el-table-column align="center" prop="pre" label="占比" width="80"></el-table-column>
                        <el-table-column align="center" prop="status_text" label="状态" width="70">
                            <template slot-scope="scope">
                                <span v-html="scope.row.status_text"></span>
                            </template>
                        </el-table-column>
                        <el-table-column prop="created_at" label="日期" width="140">
                            <template slot-scope="scope">{{ scope.row.created_at ? moment(scope.row.created_at).format('YYYY-MM-DD HH:mm') : ''}}</template>
                        </el-table-column>
                        <el-table-column label="操作" >
                            <template slot-scope="scope">
                                <el-button @click="edit(scope.row)" icon="fa fa-edit" type="primary" size="mini" plain> 编辑</el-button>
                                <el-button @click="preview(scope.row.id)" icon="fa fa-send" type="primary" size="mini" plain> 预览</el-button>
                                <el-button v-if="scope.row.status == 1" @click="status(scope.row.id)" icon="fa fa-lock" type="primary" size="mini" plain> 下架</el-button>
                                <el-button v-if="scope.row.status == 2" @click="status(scope.row.id)" icon="fa fa-unlock-alt" type="primary" size="mini" plain> 上架</el-button>
                                <el-button v-if="scope.row.id > 1" @click="destroy(scope.row.id)" icon="fa fa-trash" type="danger" size="mini" plain> 删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <pagination :pageData="listData" :params="params" @list="list"/>
                </div>
            </section>
        </el-main>
    </el-container>
</template>

<script>
    import {mapGetters} from 'vuex';
    import {ArticleList,OperateUpdate,OperateStatus,OperateDestroy,OperateAll} from '@/utils/request';
    import Pagination from '@/components/pagination';
    export default {
        components: {Pagination},
        data() {
            return {
                activeName: '0',//切换tabs
                listData: [],//列表数据
                multipleSelection:[],//多选项
                date: '',
                params: {page:1,title:'',status:'',sort:'',hot:'',lock:'',start:'',end:'',userid:''},
                admins: [],
                //日期筛选
                pickerOptions: {
                    shortcuts: [{
                        text: '今天',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '对比昨天',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近一周',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近一个月',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近三个月',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                            picker.$emit('pick', [start, end]);
                        }
                    }]
                },
            }
        },
        activated(){
            this.list();
            this.adminAll();
        },
        computed: {
            ...mapGetters(['active','loading']),
        },
        methods: {
            //切换事件
            handleClick(tab, event) {
                this.list();
            },
            //时间筛选
            pickerChange(){
                if(this.date){
                    this.params.start = this.moment(this.date[0]).format('YYYY-MM-DD');
                    this.params.end = this.moment(this.date[1]).format('YYYY-MM-DD');
                }else {
                    this.params.start = '';
                    this.params.end = '';
                }
            },
            //预览
            preview(id){

            },
            //所有角色
            adminAll(){
                OperateAll().then((res)=>{
                    this.admins = res.data;
                })
            },
            //新增
            create() {
                this.$router.push({name:'ArticleUpdate'})
            },
            //编辑
            edit(id) {
                this.$router.push({name:'ArticleUpdate',params:{id:id}})
            },
            // 更新
            update(formName){
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        OperateUpdate(this.form).then((res)=>{
                            if(res.code){
                                this.$message.error(res.msg);
                            }else{
                                this.$message({
                                    type: 'success',
                                    duration: '1000',
                                    message: res.msg,
                                    onClose:() =>{
                                        this.dialog.show = false;
                                        this.list();
                                    }
                                });
                            }
                        });
                    }
                });
            },
            // 多选删除
            multidel(){
                if(this.multipleSelection.length == 0){
                    this.$message.error('请至少选择一项你要删除的项');return false;
                }
                this.destroy(this.multipleSelection);
            },
            //删除
            destroy(id){

                let msg;
                if(id.length){
                    msg = '确定要删除选中的管理员信息吗？';
                }else{
                    msg = '确定要删除该管理员信息吗？';
                }
                this.$confirm(msg, '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    customClass: 'cms-message-box',
                    type: 'warning'
                }).then(() => {

                    OperateDestroy({id: id}).then((res) => {
                        if (res.code) {
                            this.$message.error(res.msg);
                        } else {
                            this.list();
                        }
                    });
                }).catch(() => {});
            },
            // 禁用/启用
            status(id){
                OperateStatus({id: id}).then((res)=>{
                    if (res.code) {
                        this.$message.error(res.msg);
                    } else {
                        this.list();
                    }
                })
            },
            //筛选
            search(){
                this.params.page = 1;
                this.list();
            },
            //列表
            list(){
                this.$store.dispatch('setLoading', true);
                ArticleList({params: this.params}).then((res)=>{
                    if (res.code) {
                        this.$message.error(res.msg);
                    } else {
                        this.listData = res.data;
                    }
                    this.$store.dispatch('setLoading', false);
                })
            },
            handleSelectionChange(val) {
                let select = [];
                if(val.length > 0){
                    val.filter((item)=>{
                        if(select.indexOf(item.id) === -1){
                            select.push(item.id)
                        }
                    });
                    this.multipleSelection = select;
                }else{
                    this.multipleSelection = [];
                }
            }
        }
    }
</script>

<style scoped>
    .cms-left-btn{
        float: left;
    }
.el-tab-pane{
    display: none;
}
</style>
